package com.wing.company.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.company.dao.CompanyDao;
import com.wing.company.dao.CompanyInfoDto;
import com.wing.company.model.entity.Company;
import com.wing.company.model.request.CompanyForm;
import com.wing.company.model.response.CompanyInfoVo;
import com.wing.company.model.response.CompanyVO;
import com.wing.common.utils.HttpClientaSsist;
import com.wing.common.utils.HttpHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.HttpHead;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import java.util.*;

import static java.lang.System.out;

/**
 * -ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class CompanyServiceImpl extends ServiceImpl<CompanyDao, Company> implements CompanyService {


    private static final String appkey = "0969963c8ca54046951aa74fd387a9ce";
    private static final String secretKey = "FDBD8F8C0989F84FC7D67CE57D69516C";

    private HttpClientaSsist httpClientaSsist;

    @Override
    public int add(CompanyForm companyForm) {

        //校验

        Company company =new Company();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(Company.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(companyForm, company, copyOptions);

        return baseMapper.insert(company);
    }

    @Override
    public List<CompanyVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<CompanyVO> pageList4app(Page page, Map<String, Object> params) {
        List<CompanyVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public CompanyVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public List<CompanyVO>  getQccCompanyInfo(CompanyInfoVo civo) {
        //访问企业数据
        //ResponseEntity<CompanyInfoDto> companyInfo = client.getCompanyInfo(civo.getKey(), civo.getSearchKey(), civo.getProvinceCode(), civo.getCityCode(), civo.getPageSize(), civo.getPageIndex());
        List<CompanyInfoDto> companyList = getCompanyInfo(civo.getSearchKey());
        List<CompanyVO> companyVOS = new ArrayList<>();
        if(Objects.nonNull(companyList)){
            for (CompanyInfoDto companyInfo : companyList) {
                CompanyVO companyVO = new CompanyVO();
                BeanUtil.copyProperties(companyInfo,companyVO);
                companyVOS.add(companyVO);
            }
        }

        Map<String, Object> params = null;
        params.put("key" ,civo.getKey());
        params.put("searchKey",civo.getSearchKey());
        params.put("provinceCode",civo.getProvinceCode());
        params.put("cityCode",civo.getCityCode());
        params.put("pageSize",civo.getPageSize());
        params.put("pageIndex",civo.getPageIndex());
        //查询企业表里是否存在数据
/*        if(Objects.isNull(baseMapper.list4app(params))){
            Company company =new Company();
            BeanUtil.copyProperties(companyVO,company);
            baseMapper.insert(company);
        }*/
        return companyVOS;
    }

    /**
     * 通过企查查获取企业信息
     * @param searchKey
     * @return
     */
    public List<CompanyInfoDto> getCompanyInfo(String searchKey) {
        String reqInterNme = "http://api.qichacha.com/FuzzySearch/GetList";
        String paramStr = "searchkey=".concat(searchKey);
        String status = "";
        try {
            HttpHead reqHeader = new HttpHead();
            String[] autherHeader = HttpClientaSsist.RandomAuthentHeader(appkey,searchKey);
            reqHeader.setHeader("Token", autherHeader[0]);
            reqHeader.setHeader("Timespan", autherHeader[1]);
            final String reqUri = reqInterNme.concat("?key=").concat(appkey).concat("&").concat(paramStr);
            String tokenJson = HttpHelper.httpGet(reqUri, reqHeader.getAllHeaders());
            out.println(String.format("==========================>this is response:{%s}", tokenJson));
            JSONObject jsonObject = new JSONObject(tokenJson);
            if("200".equals(jsonObject.get("Status"))){
                String result = jsonObject.getString("Result");
               List<CompanyInfoDto> companyInfoDtoList = JSON.parseArray(result,CompanyInfoDto.class);
               if(companyInfoDtoList.size() > 0){
                   log.info("解析成功:" + companyInfoDtoList.toString());
               }
            }

            status = HttpClientaSsist.FormartJson(tokenJson, "Status");
            out.println(String.format("==========================>Status:{%s}", status));
            if ("200".equals(status)) {
                HttpClientaSsist.PrettyPrintJson(tokenJson);
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        return null;
    }




}
